var asigUserClass = {
	parent: "parentId",
	pattern: "4H",
	win: null,
	form: null,

	grid: null,
    gridA: null,
    dataUser: null,
	statusGrid1: null,
    statusGrid2: null,
	layout: null,
    widgetBar:function () {
        var me = this;

        me.actionBar =  me.win.attachToolbar();
        me.actionBar.setIconsPath(mainPath + "resources/images/icons32/");
        me.actionBar.setIconSize(32);
        me.actionBar.loadXML(mainPath + "resources/xml/toolbarAsigUsu1.xml");
        me.actionBar.setAlign('right');
    },
	widgetWin: function(){
		var me = this,
            windows = new dhtmlXWindows();

		me.win = windows.createWindow('window_1', 0, 0, 580, 520);
        me.win.setText('Asignación de Municipios a Usuarios');
        me.win.setIcon('icon_09.png');
        me.win.button('park').hide();
        me.win.button('minmax1').hide();
		me.win.denyResize();
        me.win.setModal(1);
        me.win.centerOnScreen();
        me.widgetBar();

		me.layout = me.win.attachLayout('3T');
	},
    widgetForm: function(){
        var me =  this,
            panel = me.layout.cells('a'),
            str;

        panel.setHeight(80);
        panel.hideHeader();
        str = [
            { type:"settings" , labelWidth:84, inputWidth:250, position:"absolute"  },
            { type:"label", name:"form_label_1", label:"Datos del Usuario: <b>"+usuariosClass.nameUser+"</b>", width:250, labelWidth:250, labelAlign:"left", labelLeft:5, labelTop:5  },
            { type:"label", name:"form_label_2", label:"Rol de Usuario: <b>TRANSCRIPTOR</b>", width:250, labelWidth:250, labelAlign:"left", labelLeft:23, labelTop:25  },
            { type:"select", name:"sel_depto", label:"Departamento:", connector: mainPath+"index.php/dpto/combo_data", labelAlign:"left", inputWidth:190, labelLeft:28, labelTop:50, inputLeft:118, inputTop:50  },
            { type:"button" , name:"filter_btn", label:"Button", value:"Filtrar", width:"75", inputWidth:75, inputLeft:312, inputTop:48  }
        ];
        me.form = panel.attachForm(str);
    },
    onEventForm: function(){
        var me = this,
            data;

        me.form.attachEvent("onButtonClick", function(name) {
            switch(name){
                case 'filter_btn':
                    data = me.form.getFormData();
                    me.grid.groupBy(2);
                    me.grid.refreshFilters();
                    me.grid.filterBy(3,data.sel_depto);
                    me.statusGrid1.setText("Numero de Municipios Registrados: "+me.grid.getRowsNum());
                break;
            }
        });
    },
	widgetGrid: function(){
		var me = this,
            cell = me.layout.cells('b');

		cell.setWidth('290');
		cell.hideHeader();
		cell.fixSize(true, true);
		me.statusGrid1 = cell.attachStatusBar();

		me.grid = cell.attachGrid();
        me.grid.selMultiRows = true;
		me.grid.setImagePath(mainPath+'dhtmlx/imgs/');
		me.grid.setHeader("Id,Detalle,Departamento,CodDpto");
		me.grid.setColTypes("ro,ro,ro,ro");
		me.grid.setColAlign("right,left,left,left");
		me.grid.setInitWidths("30,*,*,*");
		me.grid.setColumnColor("#E5E5E5");
        me.grid.setMultiLine(false);
        me.grid.enableDragAndDrop(true);
        me.grid.setColumnHidden(2,true);
        me.grid.setColumnHidden(3,true);
		me.grid.init();

        me.grid.load(mainPath+"index.php/municipio/grid_usu", function(){
			me.grid.groupBy(2);
            me.grid.collapseAllGroups();
			me.statusGrid1.setText("Numero de Municipios Registrados: "+me.grid.getRowsNum());
            me.onLoadDataAsigUser();
		},"json");
	},
    widgetGridA: function(){
        var me = this,
            cell = this.layout.cells('c');

        cell.hideHeader();
        cell.fixSize(true, true);
        me.statusGrid2 = cell.attachStatusBar();

        me.gridA = cell.attachGrid();
        me.gridA.selMultiRows = true;
        me.gridA.setImagePath(mainPath+'dhtmlx/imgs/');

        me.gridA.setHeader("Id,Detalle,Departamento,id1");
        me.gridA.setColTypes("ro,ro,ro,ro");
        me.gridA.setColAlign("right,left,left,left");
        me.gridA.setInitWidths("30,*,50,30");
        me.gridA.setColumnColor("#E5E5E5");
        me.gridA.setColumnHidden(2,true);
        me.gridA.setColumnHidden(3,true);
        me.gridA.setMultiLine(false);
        me.gridA.enableDragAndDrop(true);
        me.gridA.init();
    },
    onLoadDataAsigUser: function(){
        var me = this,
            ind,
            id = usuariosClass.idUser;

        me.gridA.clearAll();
        me.gridA.load(mainPath+"index.php/municipio/grid_asig?usu_id="+id, function(r){
            me.gridA.groupBy(2);
            me.statusGrid2.setText("Numero de Municipios Asignados: "+me.gridA.getRowsNum());
            me.gridA.forEachRow(function(id){
                try{
                    ind = me.gridA.cellByIndex(id-1, 0).getValue();
                    me.grid.deleteRow(ind);
                } catch (e){
                }
            });
            me.statusGrid1.setText("Numero de Municipios Registrados: "+me.grid.getRowsNum());
        },"json");
    },
    onEventActionToolBar: function(){
        var me = this,
            data = Array(),
            aux;

        me.actionBar.attachEvent("onClick", function(itemId){
            switch(itemId){
                case 'btn_save':
                    dhtmlx.confirm({
                        text:"¿Esta seguro que quieres asignar los Municipios?",
                        callback: function(status){
                            if(status == true){
                                me.gridA.forEachRow(function(id){
                                    aux = {
                                        'asigu_mun': me.gridA.cells(id, 0).getValue(),
                                        'asigu_usu': usuariosClass.idUser
                                    }
                                    data.push(aux);
                                });
                                dhtmlxAjax.get(mainPath+'index.php/Asig_usu/Save_data?allData='+JSON.stringify(data),function(loader){
                                    me.statusGrid1.setText("Numero de Municipios Registrados: "+me.grid.getRowsNum());
                                    me.statusGrid2.setText("Numero de Municipios Asignados: "+me.gridA.getRowsNum());
                                    usuariosClass.grid.cells(usuariosClass.grid.getSelectedRowId(), 10).setValue(data.length);
                                    dhtmlx.alert({
                                        text:"Se asignado "+data.length+" Municipios correctamente."
                                    });
                                });
                            }
                        }
                    });
                break;
            }
        });
    },
	widgetMain: function(){
        var me = asigUserClass;

		me.widgetWin();
        me.widgetForm();
        me.onEventForm();
        me.widgetGridA();
        me.widgetGrid();
        me.onEventActionToolBar();
	}
};